﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.CompilerServices;
using AChance.Dal.Entity;
using AChance.Dal.Func;
using AChance.Func;

namespace AChance.Bll.Func
{
    public static class BllCache
    {
        internal static class Base
        {
            static Base()
            {
                Add<FDWORG>();
                //Add<FDWSDD>();
            }
            public static Dictionary<string, Info> Infos { get; set; } = new Dictionary<string, Info>();
            private static void Add<T>() where T : class
            {
                Infos.Add(typeof(T).Name, new Info(typeof(T)));
            }
            public static List<T> GetData<T>() where T : class
            {
                return Infos[typeof(T).Name].Data ??= new List<T>();
            }
            public static DateTime GetSysUpdateTime<T>() where T : class
            {
                return Infos[typeof(T).Name].SysUpdateTime;
            }
            public class Info
            {
                public Info(Type type) => Type = type;
                public Type Type { get; set; } = null;
                public DateTime SysUpdateTime { get; set; } = new DateTime();
                public DateTime SqlUpdateTime { get; set; } = new DateTime().AddSeconds(1);
                public dynamic Data { get; set; } = null;
            }
        }
        /// <summary>
        /// 机构信息
        /// </summary>
        public static class Organ
        {
            public static bool Check()
            {
                return Base.GetData<FDWORG>().Count == 1;
            }
            public static FDWORG Get()
            {
                return Base.GetData<FDWORG>()[0];
            }
            public static string GetNo()
            {
                return Check() ? Get().ORGID : BllConst.OrganNo.ORG0000000000;
            }
            public static string GetName()
            {
                return Check() ? Get().ORGNAME : FunConst.AnyChance.NAME;
            }
        }
        /// <summary>
        /// 状态信息
        /// </summary>
        public static class State
        {
            private static List<FDWSDD> _states = DalState.Base.Infos.ToList();
            private static DateTime UpdateTime { get; set; } = new DateTime();

            [MethodImpl(MethodImplOptions.Synchronized)]
            private static void Update()
            {
                if (UpdateTime == Base.GetSysUpdateTime<FDWSDD>())
                    return;
                _states = DalState.Base.Infos.ToList();
                Base.GetData<FDWSDD>().ForEach(sDDSql =>
                {
                    FDWSDD sDDFind = _states.Find(sDDState => sDDState.SDDNAME == sDDSql.SDDNAME && sDDState.SDDVALUE == sDDSql.SDDVALUE);
                    if (sDDFind != null)
                    {
                        _states.Remove(sDDFind);
                        _states.Add(sDDSql);
                    }
                });
                _states = _states.OrderBy(sDD => sDD.SDDNAME).ThenBy(sDD => sDD.SDDORDER).ToList();
                UpdateTime = Base.GetSysUpdateTime<FDWSDD>();
            }
            public static List<FDWSDD> Get()
            {
                if (UpdateTime != Base.GetSysUpdateTime<FDWSDD>())
                    Update();
                return _states;
            }
            public static string GetDispName(Enum value)
            {
                return GetDispName(value.GetTypeName(), value.GetValue());
            }
            public static string GetDispName<T>(int value) where T : Enum
            {
                return GetDispName(typeof(T).Name, value);
            }
            public static string GetDispName(string name, int value)
            {
                Predicate<FDWSDD> find = sDD => sDD.SDDNAME == name && sDD.SDDVALUE == value;
                return Get().Find(find)?.SDDDESC ?? "";
            }
        }

    }
}
